//
//  FreightPickerViewController.m
//  nonghubao
//
//  Created by Fenrir on 13-8-26.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "FreightPickerViewController.h"
#import "Helper.h"
#import "FreightTemplateViewController.h"

//  -业务逻辑细节说明-
//
//  @abstract

//  @discussion
//

//  @notes


//  -类修改日志-
//  修改人   修改时间   针对版本   修改描述

@interface FreightPickerViewController ()

@end

@implementation FreightPickerViewController
@synthesize delegate = _delegate;
@synthesize selectedFreightId = _selectedFreightId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithSelectedFreightId:(NSInteger)freightId
{
    self = [super init];
    if (self) {
        _selectedFreightId = freightId;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = _L(@"vc_title_092");
    
    UIButton* manageButton = [Helper buttonWithStyle:ButtonStyleNavigationManage];
    [manageButton addTarget:self action:@selector(manageButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:manageButton];
    
    self.addButton.alpha = 0.0f;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    if (_managing) {
        cell.accessoryView = nil;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        [self checkmarkCell:cell andIndexPath:indexPath];
    }
    return cell;
}

-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleNone;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_managing) {
        
        [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
    else {
        
        if ([_delegate respondsToSelector:@selector(freightPicked:)]) {
            [_delegate freightPicked:self.dataSource[indexPath.section]];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

#pragma mark - 私有方法
//标记被选中的cell
- (void)checkmarkCell:(UITableViewCell *)cell andIndexPath:(NSIndexPath *)indexPath
{
    
    //当用户未选择地址时，并且此地址是用户设置的默认地址，那么就标记它。
    //当用户选择地址时，标记它。
 
    if (_selectedFreightId != 0 && [self.dataSource[indexPath.section][@"id"] integerValue] == _selectedFreightId) {
        
        UIImageView* checkImageView = [[UIImageView alloc] initWithFrame:CGRectMake(290, 13, 45/2.5f, 47/2.5f)];
        
        checkImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"transaction_icon_004@2x" ofType:@"png"]];
        
        cell.accessoryView = checkImageView;
        
    }
    
    else {
        
        cell.accessoryView = nil;
    }
}

#pragma mark - 事件

- (void)manageButtonEvent
{
    _managing = !_managing;
    
    [self setViewNeedDisplay:_managing];
    
    //管理模板下改变title和右按钮
    if (_managing) {
        self.title = _L(@"vc_title_076");
        UIButton* exitManageButton = [Helper buttonWithStyle:ButtonStyleNavigationExitManage];
        [exitManageButton addTarget:self action:@selector(manageButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:exitManageButton];
    }
    else {
        self.title = _L(@"vc_title_092");
        UIButton* manageButton = [Helper buttonWithStyle:ButtonStyleNavigationManage];
        [manageButton addTarget:self action:@selector(manageButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:manageButton];
    }
    
    [self.tableView reloadData];
}

/*!
 @abstract 通过数据源是否为空来判断页面的显示
 */
- (void)viewNeedsDisplay
{
    
    if (self.dataSource.count == 0) {
        
        _blankPage = [[UIView alloc] initWithFrame:CGRectMake(0.0f, -44.0f, 320.0f, _SYS_FRAME_HEIGHT)];
        _blankPage.backgroundColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 139/2.0f, 137.0f/2.0f)];
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"transaction_img_016@2x" ofType:@"png"]];
        imageView.center = _blankPage.center;
        [_blankPage addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, imageView.frame.origin.y+imageView.frame.size.height+10.0f, 300.0f, 50.0f)];
        label.textAlignment = UITextAlignmentCenter;
        label.numberOfLines = 0;
        label.textColor = [UIColor grayColor];
        label.text = _L(@"alert_107");
        [_blankPage addSubview:label];
        
        [self.view addSubview:_blankPage];
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.view.backgroundColor = [UIColor whiteColor];
    }else{
        
        [_blankPage removeFromSuperview];
        self.tableView.backgroundColor = _SYS_COLOR_BACKGROUND;
        self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    }
}
@end
